package pro1.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import pro1.model.ResponseBody;
import pro1.model.User;
import pro1.service.UserService;

@RestController
@RequestMapping("user")
public class UserController {

	@Autowired
	UserService userService;

	/**
	 * 退出登录
	 * 
	 * @param session
	 * @return
	 */
	@GetMapping("outLogin")
	public ResponseBody outLogin(HttpSession session) {
		session.removeAttribute("user");
		return new ResponseBody(1, "成功");
	}

	/**
	 * 查询用户
	 * 
	 * @param userName
	 * @param name
	 * @param sex
	 * @param dep
	 * @param page
	 * @param num
	 * @return
	 */
	@GetMapping("allUsers")
	public ResponseBody allUsers(@RequestParam(value = "userName", required = false) String userName,
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "sex", required = false) Integer sex,
			@RequestParam(value = "dep", required = false) Integer dep,
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "num", required = false, defaultValue = "10") Integer num) {

		return userService.allUsers(userName, name, sex, dep, page, num);
	}

	/**
	 * 个人资料
	 * 
	 * @param session
	 * @return
	 */
	@GetMapping("myself")
	public ResponseBody myself(HttpSession session) {
		String userName = ((User) session.getAttribute("user")).getUsername();
		return userService.allUsers(userName, null, null, null, 1, 10);
	}

	/**
	 * 检查工号唯一
	 * 
	 * @param userName
	 * @return
	 */
	@PostMapping("checkUserName")
	public ResponseBody checkUserName(@RequestParam(value = "userName", required = true) String userName) {

		return userService.checkUserName(userName);
	}

	/**
	 * 添加或修改
	 * 
	 * @param userId
	 * @param userName
	 * @param name
	 * @param birth
	 * @param sex
	 * @param dep
	 * @return
	 */
	@PostMapping("addOrUpdateUser")
	public ResponseBody addOrUpdateUser(@RequestParam(value = "userId", required = false) Integer userId,
			@RequestParam(value = "userName", required = false) String userName,
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "birth", required = false) String birth,
			@RequestParam(value = "sex", required = false) Integer sex,
			@RequestParam(value = "dep", required = false) Integer dep,
			@RequestParam(value = "address", required = false) String address) {

		return userService.addOrUpdateUser(userId, userName, name, birth, sex, dep, address);
	}

	/**
	 * 修改自己
	 * 
	 * @param userName
	 * @param name
	 * @param birth
	 * @param sex
	 * @param dep
	 * @param address
	 * @return
	 */
	@PostMapping("updateUser")
	public ResponseBody updateUser(HttpSession session, @RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "birth", required = false) String birth,
			@RequestParam(value = "sex", required = false) Integer sex,
			@RequestParam(value = "address", required = false) String address) {
		Integer userId = ((User) session.getAttribute("user")).getId();
		return userService.addOrUpdateUser(userId, null, name, birth, sex, null, address);
	}

	/**
	 * 删除用户
	 * 
	 * @param userId
	 * @return
	 */
	@PostMapping("deleteUser")
	public ResponseBody deleteUser(@RequestParam(value = "userId", required = true) Integer userId) {

		return userService.deleteUser(userId);
	}

	/**
	 * 修改密码
	 * 
	 * @param userId
	 * @param pwd
	 * @return
	 */
	@PostMapping("updatePwd")
	public ResponseBody updatePwd(@RequestParam(value = "userId", required = true) Integer userId,
			@RequestParam(value = "pwd", required = true) String pwd) {

		return userService.updatePwd(userId, pwd);
	}

	/**
	 * 修改自己密码
	 * 
	 * @param pwd
	 * @return
	 */
	@PostMapping("updateMyPwd")
	public ResponseBody updateMyPwd(HttpSession session, @RequestParam(value = "oldPwd", required = true) String oldPwd,
			@RequestParam(value = "pwd", required = true) String pwd) {
		Integer userId = ((User) session.getAttribute("user")).getId();
		return userService.updatePwd(userId, oldPwd, pwd);
	}

	/**
	 * 查询所有管理员
	 * 
	 * @return
	 */
	@GetMapping("allAdmins")
	public ResponseBody allAdmins() {

		return userService.allAdmins();
	}

	@PostMapping("addOrUpdateAdmin")
	public ResponseBody addOrUpdateAdmin(@RequestParam(value = "userId", required = false) Integer userId,
			@RequestParam(value = "userName", required = false) String userName,
			@RequestParam(value = "pwd", required = false) String pwd,
			@RequestParam(value = "characterId", required = false) Integer characterId) {

		return userService.addOrUpdateAdmin(userId, userName, pwd, characterId);
	}

	/**
	 * 删除用户
	 * 
	 * @param userId
	 * @return
	 */
	@PostMapping("deleteAdmin")
	public ResponseBody deleteAdmin(@RequestParam(value = "userId", required = true) Integer userId) {

		return userService.deleteUser(userId);
	}

	/**
	 * 修改密码
	 * 
	 * @param userId
	 * @param pwd
	 * @return
	 */
	@PostMapping("updateAdminPwd")
	public ResponseBody updateAdminPwd(@RequestParam(value = "userId", required = true) Integer userId,
			@RequestParam(value = "pwd", required = true) String pwd) {

		return userService.updatePwd(userId, pwd);
	}
}
